/**
*@Author: QWP
*@Description: 常用方法
*@Date 2023-06-18 16:00
*/
import { message, MessageArgsProps, Modal, notification } from 'ant-design-vue'
import { NoticeType } from 'ant-design-vue/es/message';
import { NotificationArgsProps } from 'ant-design-vue/es/notification';
import { VNode, isRef } from 'vue';

/**
*@Author: QWP
*@Description: 将任何值转为数字
*@Params: val: 需转换的数据
*@Date 2023-09-11 17:10
*/
export const numberC = (val) => {
  let tvalue = `${val}`.replace(/,/g, '');
  return isNaN(Number(tvalue)) ? 0 : Number(tvalue)
};

/**
*@Author: QWP
*@Description: 判断数据是否是正确的值
*@Params: data: 需判断的数据
*@Date 2023-09-11 17:11
*/
export const isValue = (data) => {
  if(typeof(data) === 'undefined' || data === '' || data === null || data === undefined){
    return false;
  } else{
    return true
  }
}

/**
*@Author: QWP
*@Description: 将不是正确的值转为 returnval 指定的值
*@Params: data 需转换的值， returnVal：目标值
*@Date 2023-09-11 17:13
*/
export const nullC = (data, returnVal = '') => isValue(data) ? data : returnVal;

export const confirmC = () => ""

/**
*@Author: QWP
*@Description: 提示框
*@Params: msg: 提示信息  type: 图标类型
*@Date 2023-09-11 17:14
*/
export const alertC = (content: string, type = 'warning') => {
  return new Promise((resolve, reject) => {
    Modal[type]({
      title: "提示",
      content,
      autoFocusButton: null,
      okText: '确定',
      type,
      onOk(){
        resolve()
      },
      onCancel(){
        reject()
      }
    })
  })
}

/**
*@Author: QWP
*@Description: 触发通知
*@Params: message: 提示信息   params: 提示的舒心对象
*@Date 2023-09-11 17:20
*/
export const notificationC = (params: NotificationArgsProps = { message: '提示', description: '' }) =>  {
  const dparams: NotificationArgsProps = Object.assign({
    type: 'success', 
    placement: 'topRight', 
    message: '提示',
  }, params)
  const [ notificationApi ] = notification.useNotification();
  notificationApi.open(dparams)
}

/**
*@Author: QWP
*@Description: 消息提示
*@Params: message: 提示信息   params: 提示的舒心对象
*@Date 2023-09-11 17:20
*/
export const messageC = (
  content: string | VNode, 
  type: NoticeType = 'warning',
  duration: number = 5) =>  {
  message.config({ maxCount: 4 })
  message[type]( content, duration)
}

/**
*@Author: QWP
*@Description: json 深度拷贝
*@Params:
*@Date 2024-05-27 10:32
*/
export const jsonCopy = (data) => {
  if(data instanceof Object || data instanceof Array) {
    return JSON.parse(JSON.stringify(data))
  }
  return data
}


/**
*@Author: QWP
*@Description: 替代require获取文件
*@Params:
*@Date 2024-07-01 10:32
*/
export const urlFile = (path: string, type = 'assets') => {
  if(type == 'assets') {
    return new URL(`/src/assets/${path}`, import.meta.url).href
  }
  else {
    return new URL(`/src/${path}`, import.meta.url).href
  }
}

// 清除对象的值
export const clearObj = (obj) => {
  for (const key in obj) {
    if (obj[key] instanceof Array) {
      obj[key] = [];
    } else if (obj[key] instanceof String) {
      obj[key] = '';
    } else {
      obj[key] = null;
    }
  }
};

// 延迟函数
export const sleep = (time: number) => {
  return new Promise((resolve) => {
    setTimeout(() => resolve({}), time)
  })
}

// 范围函数
export const range = (min: number, max: number): number[] => {
  let min1 = min || 0;
  let max1 = max || min1;
  let results: number[] = []
  for(let i = min1; i < max1; i++) {
    results.push(i)
  }
  return results
}

// 判断两个对象是否相等
export const deepEqual =(obj1:any, obj2:any) =>{  
  // 如果两个对象引用相同，则它们是相等的  
  if (obj1 === obj2) {  
      return true;  
  }  

  // 如果其中一个对象是 null 或 undefined，另一个不是，则它们不相等  
  if (obj1 == null || obj2 == null) {  
      return false;  
  }  

  // 获取对象的类型  
  const type1 = typeof obj1;  
  const type2 = typeof obj2;  

  // 如果类型不同，则它们不相等  
  if (type1 !== type2 || Array.isArray(obj1) !== Array.isArray(obj2)) {  
      return false;  
  }  

  // 如果两个对象都是日期类型，则比较它们的值  
  if (obj1 instanceof Date && obj2 instanceof Date) {  
      return obj1.getTime() === obj2.getTime();  
  }  

  // 如果两个对象都是正则表达式，则比较它们的模式和修饰符  
  if (obj1 instanceof RegExp && obj2 instanceof RegExp) {  
      return obj1.toString() === obj2.toString();  
  }  

  // 对于其他对象类型，比较它们的键和值  
  const keys1 = Object.keys(obj1);  
  const keys2 = Object.keys(obj2);  

  // 如果键的数量不同，则它们不相等  
  if (keys1.length !== keys2.length) {  
      return false;  
  }  

  // 检查每个键和对应的值是否相等  
  for (let key of keys1) {  
      if (  
          !keys2.includes(key) ||  
          !deepEqual(obj1[key], obj2[key])  
      ) {  
          return false;  
      }  
  }  

  return true;  
} 

// 判断是否是Ref 并取值
export const isRefC = (data: any) => {
  if(isRef(data)) {
    return data.value
  } 
  else {
    return data
  }
}

/**
 * 对象数组去重
 * @param data 对象集合
 * @param key 去重对象的关键键名
 */
export const setArray = (data: Array<any>, key: string) => {
  let newArr = [];
  for (let i = 0; i < data.length; i++) {
    if (newArr.indexOf(data[i][key]) == -1) {
      newArr.push(data[i][key]);
    } else {
      data.splice(i, 1);
      i--;
    }
  }
  return data;
}

export default{
  numberC,
  isValue,
  nullC,
  alertC,
  notificationC,
  jsonCopy,
  urlFile,
  sleep,
  range,
  deepEqual
};